package com.apestech.formwork.stream;

import java.util.ArrayList;

import com.alibaba.fastjson.JSON;
import com.apestech.framework.db.SqlResultSet;
import com.apestech.framework.db.callback.QueryCallBackHandler;
import com.apestech.framework.json.JSONArray;
import com.apestech.framework.stream.callback.ReadRowHander;

public class ArrayObjectStreamHander implements  ReadRowHander {
	
	private Object obj;
	private ReadRowHander readRowHander=null;
	
	
	public ArrayObjectStreamHander(Object obj){
		this.obj=obj;
	}
	
	public ArrayObjectStreamHander(Object obj, ReadRowHander readRowHander){
		this.obj=obj;
		this.readRowHander=readRowHander;
	}

	public String processRow(int index) throws Exception {
		String text="";
		if(readRowHander!=null){
			text=readRowHander.processRow(index);
		} else {
			if(this.obj instanceof SqlResultSet){
				text=JSON.toJSONString(((SqlResultSet)this.obj).toRowJson());
			} else if(this.obj instanceof JSONArray){
				text=JSON.toJSONString(((JSONArray)this.obj).get(index));
			} else if(this.obj instanceof ListStream){
				text=((ListStream)this.obj).getText(index);
			} else if(this.obj instanceof ArrayList<?>){
				text=((ArrayList<?>)this.obj).get(index).toString();
			} else if(this.obj instanceof String[]){
				text=((String[])this.obj)[index];
			} else if(this.obj instanceof String[][]){
				text=((String[][])this.obj)[index].toString();
			}
		}
		if(index>0){
			text=","+text;
		}
		return text;
	}

}
